<?php defined('SYSPATH') or die('No direct script access.');
	
	class Model_Services_Statistics extends Model implements Jqgrid_GridListener{
	
				
		public function count($criteria = false){
			$result = DB::select(array('COUNT("*")', 'num_records'))->from('averages_view')->execute()->current();
			return $result["num_records"];
		}
			
		public function getList($criteria = false){
			
			if (!$criteria){
				$averages_list = DB::select()->from('averages_view')->execute()->as_array();
			}else{
				
				if ($criteria->isSearch){
					
					$averages_list = DB::select()->from('averages_view')
									->where($criteria->searchField, $criteria->getSQLOper(), $criteria->getSQLSearchString())
									->order_by($criteria->sidx,$criteria->sord)
									->limit($criteria->limit)
									->execute()
									->as_array();
							
				}else{
					$averages_list = DB::select()->from('averages_view')->order_by($criteria->sidx,$criteria->sord)->limit($criteria->limit)->execute()->as_array(); 
				}
			}
						
		
			$dataset = array();
			$precision = 3;
			foreach ($averages_list as $average_row){
				$dataset[] = array(
									$average_row["decade"],
									($average_row["sex"] == "M")? "Masculino" : "Femenino",
									round(floatval($average_row["aod_avg"]),$precision),
									round(floatval($average_row["aod_trust"]),$precision),
									round(floatval($average_row["aos_avg"]),$precision),
									round(floatval($average_row["aos_trust"]),$precision),
									round(floatval($average_row["air_avg"]),$precision),
									round(floatval($average_row["iar_trust"]),$precision),
									round(floatval($average_row["amount"]),$precision)
							);
			}
			
			return $dataset;
		}
		
		
		public function getAODStats($criteria = false, $sex = 'Masculino'){
			$dataset = $this->getList($criteria);
			$aod_dataset = array();
			
						
			foreach($dataset as $average_row){
				if ($average_row[1] == $sex){
												
				}
				
			}
			
			
			
			
			
			
		}
		
		
		/**
		*
		* get all surgeries from an id
		* @param int $minId
		* @param int $maxAmount
		* @return array of surgery
		*/
		public function getListFromMinID($minId, $maxAmount){
			//return ORM::factory("dao_surgery")->where("id",">",$minId)->order_by("id", "ASC")->limit($maxAmount)->find_all()->as_array();
		}
	
		public function get($id){}
		public function save($id, array $data){}
		public function delete($id){}
		
	}
?>
